package com.lewei.print;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;

import com.lewei.utils.Constants;
/**
 * 
 * OL25数据打印模型
 * @author 22901
 *
 */
public class OL25 implements Printable{
	/** 标签样式 */
	private String lenFlag="E";
	/**试验单位 */
	private  String testingAgency="ETC";
	/**日期 */
	private  String prodDatea="2017-02-09";
	
	/**编号 */
	private  String typeNo="900540G23R6SA15N";
	
	/**出厂编号 */
	private  String serialNo="200052-951";
	
	/**限速器名称 */
	private  String elevatorNo="OL25";
	
	/**Vns */
	private  String vns="1.60";
	
	/**Vcs */
	private  String vcs="1.84";
	
	/**Vts */
	private  String vts="2.00";
	
	/**钢丝绳粗细 */
	private  String lineThickness="6.0";
	
	/**FbrakeMin */
	private  String fbrakeMin="500";
	
	/** 标签样式 */
	private String labelType="EN81";
	
	/**型号 */
	private String modelNo="1018300";
	
	/**证书编号 */
	private String certificateNo="TUV-A-AT-1-99-002-CEGB-6";

	/** 左边距 */
	private double paddingLeft = 0;

	/** 上边距 */
	private double paddingTop = 0;

	/** 打印机名称 */
	private String printerName = "Foxit Reader Plus Printer";
	
	
	
	

	public String getTestingAgency() {
		return testingAgency;
	}


	public void setTestingAgency(String testingAgency) {
		this.testingAgency = testingAgency;
	}


	public String getLenFlag() {
		return lenFlag;
	}


	public void setLenFlag(String lenFlag) {
		this.lenFlag = lenFlag;
	}


	public String getProdDatea() {
		return prodDatea;
	}


	public void setProdDatea(String prodDatea) {
		this.prodDatea = prodDatea;
	}

	public String getTypeNo() {
		return typeNo;
	}

	public void setTypeNo(String typeNo) {
		this.typeNo = typeNo;
	}
	
	public String getSerialNo() {
		return serialNo;
	}
	
	public void setSerialNo(String serialNo) {
		this.serialNo = serialNo;
	}
	
	public String getElevatorNo() {
		return elevatorNo;
	}

	public void setElevatorNo(String elevatorNo) {
		this.elevatorNo = elevatorNo;
	}

	public String getVns() {
		return vns;
	}

	public void setVns(String vns) {
		this.vns = vns;
	}

	public String getVcs() {
		return vcs;
	}

	public void setVcs(String vcs) {
		this.vcs = vcs;
	}

	public String getVts() {
		return vts;
	}

	public void setVts(String vts) {
		this.vts = vts;
	}

	public String getLineThickness() {
		return lineThickness;
	}


	public void setLineThickness(String lineThickness) {
		this.lineThickness = lineThickness;
	}

	public String getFbrakeMin() {
		return fbrakeMin;
	}

	public void setFbrakeMin(String fbrakeMin) {
		this.fbrakeMin = fbrakeMin;
	}

	public String getLabelType() {
		return labelType;
	}


	public void setLabelType(String labelType) {
		this.labelType = labelType;
	}

	public String getModelNo() {
		return modelNo;
	}

	public void setModelNo(String modelNo) {
		this.modelNo = modelNo;
	}

	public String getCertificateNo() {
		return certificateNo;
	}

	public void setCertificateNo(String certificateNo) {
		this.certificateNo = certificateNo;
	}

	public double getPaddingLeft() {
		return paddingLeft;
	}


	public void setPaddingLeft(double paddingLeft) {
		this.paddingLeft = paddingLeft;
	}


	public double getPaddingTop() {
		return paddingTop;
	}


	public void setPaddingTop(double paddingTop) {
		this.paddingTop = paddingTop;
	}


	public String getPrinterName() {
		return printerName;
	}


	public void setPrinterName(String printerName) {
		this.printerName = printerName;
	}
	/**
	 * 设置边距。
	 * 
	 * @param paddingLeft
	 * @param paddingTop
	 */
	public void setPadding(double paddingLeft, double paddingTop) {
		this.paddingLeft = paddingLeft;
		this.paddingTop = paddingTop;
	}

	public OL25(String printerName) {
		super();
		this.printerName = printerName;
	}

	public OL25() {
		super();
	}

	/**
	 * @param Graphic指明打印的图形环境
	 * @param PageFormat指明打印页格式（页面大小以点为计量单位，1点为1英才的1/72，1英寸为25.4毫米。A4纸大致为595×
	 *            842点）
	 * @param pageIndex指明页号
	 **/

	@Override
	public int print(Graphics gra, PageFormat pf, int pageIndex)
			throws PrinterException {
		// TODO Auto-generated method stub
	         	 Component c = null;
				// 转换成Graphics2D
				Graphics2D g2 = (Graphics2D) gra;
				// 设置打印颜色为黑色
				g2.setColor(Color.black);

//				 打印起点坐标
				 double x = pf.getImageableX()+50;
				 double y = pf.getImageableY();

				switch (pageIndex) {
				case 0:
					if(lenFlag.equals("E")){
						Font font = new Font("Calibri", Font.BOLD, 9);
						g2.setFont(font);
						g2.drawString(typeNo, (float) (x+ 15),
								(float) (y+20));
						 String filename = "C:\\toolsZ\\labelPrint\\codeZ\\typeNo.jpg";
						 Image Img = null;
						 try {
							 Img = ImageIO.read(new File(filename));
						 } catch (IOException e) {
							 // TODO Auto-generated catch block
							 e.printStackTrace();
						 }
						 g2.drawImage(Img,(int)x+125,(int)y+15,55,55,c);
						 
						 g2.drawString(serialNo, (float) (x+ 70),
									(float) (y+60));
						 filename = "C:\\toolsZ\\labelPrint\\codeZ\\serialNo.jpg";
						 try {
							 Img = ImageIO.read(new File(filename));
						 } catch (IOException e) {
							 // TODO Auto-generated catch block
							 e.printStackTrace();
						 }
						 g2.drawImage(Img,(int)x-5,(int)y+40,55,55,c);
						 
						 g2.drawString(elevatorNo, (float) (x+ 90),
									(float) (y+90));
						 
						 font = new Font("Calibri", Font.BOLD, 9);
						 g2.setFont(font);
						 
						 g2.drawString(vns, (float) (x -27),
									(float) (y+115));
						 g2.drawString(vcs, (float) (x +26),
									(float) (y+115));
						 g2.drawString(vts, (float) (x+ 76),
									(float) (y+115));
						 
						font = new Font("Calibri", Font.BOLD, 8);
						g2.setFont(font);
						 g2.drawString(lineThickness, (float) (x+ 148),
									(float) (y+110));
						 font = new Font("Calibri", Font.BOLD, 9);
							g2.setFont(font);
						 g2.drawString(elevatorNo, (float) (x-32),
									(float) (y+75));
						 font = new Font("Calibri", Font.BOLD, 5);
							g2.setFont(font);
						 g2.drawString(prodDatea, (float) (x-30),
									(float) (y+22));
							 filename = "C:\\toolsZ\\labelPrint\\codeZ\\wittur.gif";
							 try {
								 Img = ImageIO.read(new File(filename));
							 } catch (IOException e) {
								 // TODO Auto-generated catch block
								 e.printStackTrace();
							 }
							 g2.drawImage(Img,(int)x-32,(int)y+33,24,20,c);
							 
							 font = new Font("Calibri", Font.BOLD, 7);
								g2.setFont(font);
							 g2.drawString(elevatorNo, (float) (x+230),
										(float) (y+46));
							 g2.drawString(certificateNo, (float) (x+210),
										(float) (y+57));
							 g2.drawString(prodDatea, (float) (x+230),
										(float) (y+73));
							 
					//1018300中文标签
					}else if(lenFlag.equals("G")){
						Font font = new Font("Calibri", Font.BOLD, 9);
						g2.setFont(font);
						
						 String filename = "C:\\toolsZ\\labelPrint\\codeZ\\DtypeNo.jpg";
						 Image Img = null;
						 try {
							 Img = ImageIO.read(new File(filename));
						 } catch (IOException e) {
							 // TODO Auto-generated catch block
							 e.printStackTrace();
						 }
						 g2.drawImage(Img,(int)x+5,(int)y+20,150,30,c);
						 
						 filename = "C:\\toolsZ\\labelPrint\\codeZ\\DserialNo.jpg";
						 try {
							 Img = ImageIO.read(new File(filename));
						 } catch (IOException e) {
							 // TODO Auto-generated catch block
							 e.printStackTrace();
						 }
						 g2.drawImage(Img,(int)x+33,(int)y+57,110,20,c);
						 
						 
						 font = new Font("Calibri", Font.BOLD, 9);
						 g2.setFont(font);
						 
						 g2.drawString(vns, (float) (x -27),
									(float) (y+115));
						 g2.drawString(vcs, (float) (x +26),
									(float) (y+115));
						 g2.drawString(vts, (float) (x+ 76),
									(float) (y+115));
						 
						
						g2.drawString(lineThickness, (float) (x+140),
								(float) (y+102));
						 g2.drawString(elevatorNo, (float) (x-32),
									(float) (y+75));
						 font = new Font("Calibri", Font.BOLD, 5);
							g2.setFont(font);
						 g2.drawString(prodDatea, (float) (x-32),
									(float) (y+35));
						 
						 //设置字体方向
						 g2.rotate(Math.toRadians(270));
						
						 font = new Font("Calibri", Font.BOLD, 7);
						 g2.setFont(font);
						 g2.drawString(elevatorNo,(float) (-(y+60)),
									(float) (x+210));
						 g2.drawString(certificateNo,(float) (-(y+103)),
									(float) (x+222));
					}
				
					return PAGE_EXISTS;
				default:
					return NO_SUCH_PAGE;
				}
	}
	
	public static void main(String[] args) {

		// 通俗理解就是书、文档
		Book book = new Book();
		// 设置成竖打
		PageFormat pf = new PageFormat();
		pf.setOrientation(PageFormat.PORTRAIT);
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper p = new Paper();
		p.setSize(350, 210);// 纸张大小
		// A4(595 X 842)设置打印区域，其实0，0应该是72，72，因为A4纸的默认X,Y边距是72
		p.setImageableArea(0, 0, 350, 210);
		pf.setPaper(p);
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(new OL25(), pf);

		// 获取打印服务对象
		PrinterJob job = PrinterJob.getPrinterJob();

		HashAttributeSet hs = new HashAttributeSet();
		
		String printerName = "HP LaserJet Professional P1106";
		//String printerName = "Send To OneNote 2016";
		hs.add(new PrinterName(printerName, null));

		PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, hs);

		try {
			job.setPrintService(pss[0]);
		} catch (PrinterException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// 设置打印类
		job.setPageable(book);

		try {
			// 可以用printDialog显示打印对话框，在用户确认后打印；也可以直接打印
			// boolean a=job.printDialog();
			// if(a)
			// {
			job.print();
			// }
		} catch (PrinterException e) {
			e.printStackTrace();
		}
	}
	
	public void printcode() {

		// 通俗理解就是书、文档
		Book book = new Book();

		PageFormat pageFormat = new PageFormat();
		// 打印方向
		pageFormat.setOrientation(PageFormat.PORTRAIT);
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper paper = new Paper();
		paper.setSize(350, 220);// 纸张大小
		if(lenFlag.equals("E")){
			// A4(595 X 842)设置打印区域，其实0，0应该是72，72，因为A4纸的默认X,Y边距是72
			paper.setImageableArea(Constants.LABELLEFT1018300E, Constants.LABELTOP1018300E,  350, 220);
		}else{
			paper.setImageableArea(Constants.LABELLEFT1018300, Constants.LABELTOP1018300-5,  350, 220);
		}
		
		pageFormat.setPaper(paper);
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(this, pageFormat);

		// 获取打印服务对象
		PrinterJob printJob = PrinterJob.getPrinterJob();

		HashAttributeSet hashAttributeSet = new HashAttributeSet();

		hashAttributeSet.add(new PrinterName(printerName, null));

		PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, hashAttributeSet);

		try {
			printJob.setPrintService(printServices[0]);
		} catch (PrinterException e1) {
			e1.printStackTrace();
			throw new RuntimeException(e1.getMessage());
		}

		// 设置打印类
		printJob.setPageable(book);
		try {
			// 可以用printDialog显示打印对话框，在用户确认后打印；也可以直接打印
			// boolean a=job.printDialog();
			// if(a)
			// {
			printJob.print();
			// }
		} catch (PrinterException e) {
			e.printStackTrace();
		}

	}
	
	
	
}
